#version 410 core
struct Material
{
	// 漫反射贴图
	sampler2D   diffuse;
	// 镜面光照的颜色
	sampler2D  specular;
	// 反光度
	// 越大  亮点越小 光束越集中
	float shininess;
};

// 定向光源
struct DirectionLight
{
	vec3 direction;
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};

// 点光源
struct PointLight
{
	vec3 position;
	
	// 常数项
	float constant;
	// 线性衰减
    float linear;
	// 二次衰减 
    float quadratic;

	vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

struct Spotlight {
    vec3  position;
    vec3  direction;
    float cutOff;
    float outerCutOff;

	// 常数项
	float constant;
	// 线性衰减
    float linear;
	// 二次衰减 
    float quadratic;

	vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;
uniform vec3 viewPos;
uniform Material material;
uniform DirectionLight light;
uniform Spotlight spotLight;

#define NR_POINT_LIGHTS 4
uniform PointLight pointLights[NR_POINT_LIGHTS];


vec3 calcDirLight(DirectionLight light, Material material, vec3 normal, vec3 viewDir, vec2 texCoords);
vec3 calcPointLight(PointLight light, Material material, vec3 normal, vec3 fragPos, vec3 viewDir, vec2 texCoords);
vec3 calcSpotLight(Spotlight light, Material material, vec3 normal, vec3 fragPos, vec3 viewDir, vec2 texCoords);


void main()
{
	vec3 norm = normalize(Normal);
	
	vec3 viewDir = normalize(viewPos - FragPos);

	// 定向光
	vec3 resultDirLight  = calcDirLight(light, material, norm, viewDir, TexCoords);

	// 点光源
	vec3 resultPoint = vec3(0.f);
	for(int i = 0; i < NR_POINT_LIGHTS; i++)
	{
		resultPoint += calcPointLight(pointLights[i], material, norm, FragPos, viewDir, TexCoords);   
	}

	// 聚光灯
	vec3 resultSpot = calcSpotLight(spotLight, material, norm, FragPos, viewDir, TexCoords);

	FragColor = vec4(resultDirLight + resultPoint + resultSpot, 1.0);
}

vec3 calcDirLight(DirectionLight light, Material material, vec3 normal, vec3 viewDir, vec2 texCoords)
{
	vec3 lightDir = normalize(-light.direction);
	// 漫反射着色
	float diff = max(dot(normal, lightDir), 0.0);
	// 镜面光着色
	vec3 reflectDir = reflect(-lightDir, normal);
	float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
	// 合并结果
	vec3 ambient  = light.ambient  * vec3(texture(material.diffuse, texCoords));
	vec3 diffuse  = light.diffuse  * diff * vec3(texture(material.diffuse, texCoords));
	vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoords));

	return (ambient + diffuse + specular);
}

vec3 calcPointLight(PointLight light, Material material, vec3 normal, vec3 fragPos, vec3 viewDir, vec2 texCoords)
{
    vec3 lightDir = normalize(light.position - fragPos);
    // 漫反射着色
    float diff = max(dot(normal, lightDir), 0.0);
    // 镜面光着色
    vec3 reflectDir = reflect(-lightDir, normal);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    // 衰减
    float distance    = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * distance + 
                 light.quadratic * (distance * distance));    
    // 合并结果
    vec3 ambient  = light.ambient  * vec3(texture(material.diffuse, texCoords));
    vec3 diffuse  = light.diffuse  * diff * vec3(texture(material.diffuse, texCoords));
    vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoords));
    ambient  *= attenuation;
    diffuse  *= attenuation;
    specular *= attenuation;
	vec3 ret =(ambient + diffuse + specular);

	return (ambient + diffuse + specular);
}

vec3 calcSpotLight(Spotlight light, Material material, vec3 normal, vec3 fragPos, vec3 viewDir, vec2 texCoords)
{
	vec3 lightDir = normalize(light.position - fragPos);

	float theta = dot(lightDir, normalize(-light.direction));
	float epsilon   = light.cutOff - light.outerCutOff;
	float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);   
	   
	// 漫反射着色
	float diff = max(dot(normal, lightDir), 0.0);
	// 镜面光着色
	vec3 reflectDir = reflect(-lightDir, normal);
	float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

	float distance = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); 

	// 合并结果
	vec3 ambient  = light.ambient  * vec3(texture(material.diffuse, texCoords));
	vec3 diffuse  = light.diffuse  * diff * vec3(texture(material.diffuse, texCoords));
	vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoords));
  
	ambient *= attenuation * intensity;
    diffuse *= attenuation * intensity;
    specular *= attenuation * intensity;
	return (ambient + diffuse + specular);
}